package com.yishun.springboot.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.RequestGongWuShu.TspDistrictlevel;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yinquan.util.DataUtil;
import com.yinquan.util.ResultUtils;
import com.yinquan.util.TimeUtil;
import com.yishun.springboot.mapper.RecordNumMapper;
import com.yishun.springboot.mapper.TspGetDataMapper;
import com.yishun.springboot.pojo.GeneralRecordNumBeans;
import com.yishun.springboot.pojo.GwsLonggangquTspDistrictlevel;
import com.yishun.springboot.pojo.RequestGongWuShuTspBaseInfo;
import com.yishun.springboot.pojo.RequestGongWuShuTspRealTimeData;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j

/**
 * 定时推送tsp数据到区级工务署 以及推送设备基本信息
 *
 * @author GA
 *
 */

@Component
public class GongWuSuTspDistrictlevelService {

	@Autowired
	TspGetDataMapper tspGetDataMapper;

	@Autowired
	RecordNumMapper recordNumMapper;

	public String updateGongWuShuTspDetailedData(String eng_code, String project_code, String api_key,
			String deviceCode, String projectName, Map paramMap) {
		String dateTime = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
		log.info("开始推送tsp基础信息到区工务署，当前时间：" + dateTime);

		TspDistrictlevel districtlevel = new TspDistrictlevel();

		RequestGongWuShuTspBaseInfo r = tspGetDataMapper.findRecentlyBaseInfoOneSendGWSTow(deviceCode);
		Map<String, Object> hashMap = new HashMap<>();
		hashMap.put("device_installation_date", r.getDevice_installation_date());
		hashMap.put("install_address", r.getInstall_address());
		hashMap.put("name", r.getName());
		hashMap.put("type", r.getType());
		hashMap.put("install_company", paramMap.get("installCompany").toString());
		hashMap.put("device_no", r.getDevice_no());

		String res = districtlevel.addBasicInfo(api_key, project_code, eng_code, hashMap);

		JSONObject jsonObject = JSONObject.parseObject(res);

		String code = jsonObject.getString("code");

		if (!code.equals("0")) {
			return JSONObject.toJSONString(ResultUtils.jsonFail("TSP基础信息推送失败"), SerializerFeature.WriteMapNullValue);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectName", projectName);
		map.put("deviceCode", deviceCode);
		map.put("apiKey", api_key);
		map.put("engCode", eng_code);
		map.put("projectCode", project_code);
		map.put("deviceNo", r.getDevice_no());
		map.put("createTime", r.getDevice_installation_date());
		map.put("version", paramMap.get("version").toString());
		int addRes = tspGetDataMapper.addDistrictlevel(map);

		if (addRes < 1) {
			log.info("数据库新增失败，会导致无法上传TSP实时数据");
			return JSONObject.toJSONString(ResultUtils.jsonFail("新增数据失败"), SerializerFeature.WriteMapNullValue);
		}

		return JSONObject.toJSONString(ResultUtils.jsonSuccess(), SerializerFeature.WriteMapNullValue);
	}

	@Async
	@Scheduled(fixedRate = 1000 * 60)
	public void timingPushTspDistrictLevelTsp() {
		log.info("进入定时推送本机tsp实时数据到区工务署..。");
		String timestamp = DataUtil.transferLongToDate("yyyy-MM-dd HH:mm:ss", new Date().getTime());
		log.info("定时输出，当前时间为：" + timestamp);

		TspDistrictlevel districtlevel = new TspDistrictlevel();
		// 接收结果集
		RequestGongWuShuTspRealTimeData r = new RequestGongWuShuTspRealTimeData();
		String code = "";// 设备号

		String apiKey = "";

		String projectCode = "";

		String engCode = "";

		String apiVersion = "";

		String resultStr = "";// 接收工务署返回值

		int insertRecordNum = 0;// 记录本次新增数据量

		JSONObject resultJsonObject = new JSONObject();// 接收转换后得工务署返回值

		String message = "";// 接收工务署msg

		// 查询出推送给区工务署的设备基本信息
		List<GwsLonggangquTspDistrictlevel> gwsLonggangquTspDistrictlevelList = tspGetDataMapper.findByCode();

		GwsLonggangquTspDistrictlevel gwsLonggangquTspDistrictlevel = new GwsLonggangquTspDistrictlevel();

		for (int i = 0; i < gwsLonggangquTspDistrictlevelList.size(); i++) {

			gwsLonggangquTspDistrictlevel = gwsLonggangquTspDistrictlevelList.get(i);

			code = gwsLonggangquTspDistrictlevel.getDevice_code();

			apiKey = gwsLonggangquTspDistrictlevel.getApi_key();

			projectCode = gwsLonggangquTspDistrictlevel.getProject_code();

			engCode = gwsLonggangquTspDistrictlevel.getEng_code();

			apiVersion = gwsLonggangquTspDistrictlevel.getApi_version();

			if (StringUtils.isEmpty(code) || StringUtils.isEmpty(apiKey) || StringUtils.isEmpty(projectCode)
					|| StringUtils.isEmpty(engCode) || StringUtils.isEmpty(apiVersion)) {

				continue;
			}

			r = tspGetDataMapper.findRecentlyRealTimeOneSendGWS(code);

			resultStr = districtlevel.addRecord(apiKey, projectCode, engCode, r);

			resultJsonObject = JSONObject.parseObject(resultStr);

			message = resultJsonObject.getString("message");

			if (message.equals("成功")) {
				log.info("本次新增成功");
				insertRecordNum++;
			}
		}
		log.info("tsp实时数据共新增：" + insertRecordNum);

		// 插入条数
		GeneralRecordNumBeans generalRecordNumBeans = new GeneralRecordNumBeans();
		log.info("一輪完成！进入mysql插入本次成功条数！");
		TimeUtil t = new TimeUtil();
		String currTime = t.timeUtil2String();
		generalRecordNumBeans.setRecord_data(currTime);
		generalRecordNumBeans.setRecord_num(insertRecordNum);
		recordNumMapper.innerOneRecord(generalRecordNumBeans);

	}

}
